package view.theme
{
	import flash.display.BitmapData;
	import flash.text.TextFormat;
	
	import embeds.EmbedRes;
	
	import enums.ColorLib;
	
	import feathers.controls.Label;
	import feathers.controls.Screen;
	import feathers.controls.ScrollText;
	import feathers.controls.TextArea;
	import feathers.controls.text.StageTextTextEditor;
	import feathers.controls.text.TextFieldTextRenderer;
	import feathers.core.DisplayListWatcher;
	import feathers.core.FeathersControl;
	import feathers.display.TiledImage;
	import feathers.system.DeviceCapabilities;
	
	import starling.core.Starling;
	import starling.display.DisplayObjectContainer;
	import starling.events.Event;
	import starling.events.ResizeEvent;
	import starling.textures.Texture;
	import starling.textures.TextureAtlas;
	import starling.utils.Color;
	
	
	/**
	 * 自定义主题
	 * @author £iang
	 * 
	 */	
	public class CustomTheme extends DisplayListWatcher
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** iPhone dpi */
		protected static const ORIGINAL_DPI_IPHONE_RETINA:int = 326;
		/** iPad dpi */
		protected static const ORIGINAL_DPI_IPAD_RETINA:int = 264;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		/** 原始dpi */
		protected var _originalDPI:int;
		/** 缩放比 */
		protected var scale:Number = 1;
		
		/** 小标题文本格式 */
		protected var titleTextFormat:TextFormat;
		/** 居中小标题文本格式 */
		protected var centerTitleTextFormat:TextFormat;
		/** 大标题文本格式 */
		protected var largeTitleTextFormat:TextFormat;
		/** 居中大标题文本格式 */
		protected var largeCenterTitleTextFormat:TextFormat;
		/** 内容文本格式 */
		protected var contentTextFormat:TextFormat;
		/** 居中内容文本格式 */
		protected var centerContentTextFormat:TextFormat;
		/** 重要内容文本格式 */
		protected var importantContentTextFormat:TextFormat;
		
		/** 主题图集 */
		protected var atlas:TextureAtlas;
		/** 主题图集BitmapData */
		protected var atlasBitmapData:BitmapData;
		
		/** 背景图片 */
		protected var primaryBackground:TiledImage;
		/** 背景贴图纹理 */
		protected var primaryBackgroundTexture:Texture;
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		protected function initialize():void
		{
			const scaledDPI:int = DeviceCapabilities.dpi / Starling.contentScaleFactor;
			this._originalDPI = scaledDPI;
			
			/*if(DeviceCapabilities.isTablet(Starling.current.nativeStage))
			{
				this._originalDPI = ORIGINAL_DPI_IPAD_RETINA;
			}
			else
			{
				this._originalDPI = ORIGINAL_DPI_IPHONE_RETINA;
			}*/
			
			if(!DeviceCapabilities.isTablet(Starling.current.nativeStage))
			{
				this._originalDPI = ORIGINAL_DPI_IPHONE_RETINA;
			}
		
			
			//this._originalDPI = ORIGINAL_DPI_IPHONE_RETINA;
			this.scale = scaledDPI / this._originalDPI;
			
			FeathersControl.defaultTextRendererFactory = textRendererFactory;
			FeathersControl.defaultTextEditorFactory = textEditorFactory;
			
			const fontNames:String = "微软雅黑,宋体";
			
			this.titleTextFormat = new TextFormat( fontNames, 24 * this.scale, Color.WHITE, true );
			this.centerTitleTextFormat = 
				new TextFormat( fontNames, 36 * this.scale, 
					Color.WHITE, true, false, false, null, null, "center" );
			this.largeTitleTextFormat = new TextFormat( fontNames, 48 * this.scale, Color.WHITE, true );
			this.largeCenterTitleTextFormat = 
				new TextFormat( fontNames, 48 * this.scale, 
					Color.WHITE, true, false, false, null, null, "center" );
			this.contentTextFormat = new TextFormat( fontNames, 24 * this.scale, Color.WHITE );
			this.centerContentTextFormat = new TextFormat( fontNames, 24 * this.scale, Color.WHITE, false, false, false, null, null, "center" );
			this.importantContentTextFormat = new TextFormat( fontNames, 24 * this.scale, ColorLib.IMPORTANT );
			
			/*PopUpManager.overlayFactory = popUpOverlayFactory;
			Callout.stagePaddingTop = Callout.stagePaddingRight = Callout.stagePaddingBottom =
				Callout.stagePaddingLeft = 16 * this.scale;*/
			
			var imageClass:Class = EmbedRes.ATLAS_IMAGE_THEME;
			var xmlClass:Class = EmbedRes.ATLAS_XML_THEME;
			
			const atlasBitmapData:BitmapData = ( new imageClass() ).bitmapData;
			this.atlas = new TextureAtlas(Texture.fromBitmapData(atlasBitmapData, false), XML( new xmlClass() ));
			if(Starling.handleLostContext)
			{
				this.atlasBitmapData = atlasBitmapData;
			}
			else
			{
				atlasBitmapData.dispose();
			}
			
			this.primaryBackgroundTexture = this.atlas.getTexture("primary-background");
			
			/*const backgroundSkinTexture:Texture = this.atlas.getTexture("background-skin");
			const backgroundDownSkinTexture:Texture = this.atlas.getTexture("background-down-skin");
			const backgroundDisabledSkinTexture:Texture = this.atlas.getTexture("background-disabled-skin");
			const backgroundFocusedSkinTexture:Texture = this.atlas.getTexture("background-focused-skin");*/
			
			if(this.root.stage)
			{
				this.initializeRoot();
			}
			else
			{
				this.root.addEventListener(Event.ADDED_TO_STAGE, root_addedToStageHandler);
			}
			
			this.setInitializerForClassAndSubclasses( Screen, screenInitializer );
			this.setInitializerForClass( Label, titleLabelInitializer, "smallTitleLabel" );
			this.setInitializerForClass( Label, centerTitleLabelInitializer, "smallCenterTitleLabel" );
			this.setInitializerForClass( Label, largeTitleLabelInitializer, "largeTitleLabel" );
			this.setInitializerForClass( Label, largeCenterTitleLabelInitializer, "largeCenterTitleLabel" );
			this.setInitializerForClass( Label, importantContentLabelInitializer, "importantContentLabel" );
			this.setInitializerForClass( Label, centerContentLabelInitializer, "centerContentLabel" );
			this.setInitializerForClass( Label, contentLabelInitializer );
			this.setInitializerForClass( TextArea, textAreaInitializer );
			this.setInitializerForClass( ScrollText, scrollTextInitializer );
		}
		
		/**
		 * 背景贴图 
		 * 
		 */		
		protected function initializeRoot():void
		{
			this.primaryBackground = new TiledImage(this.primaryBackgroundTexture);
			this.primaryBackground.width = root.stage.stageWidth;
			this.primaryBackground.height = root.stage.stageHeight;
			this.root.addChildAt(this.primaryBackground, 0);
			this.root.stage.addEventListener(ResizeEvent.RESIZE, stage_resizeHandler);
			this.root.addEventListener(Event.REMOVED_FROM_STAGE, root_removedFromStageHandler);
		}
		
		/**
		 * 文本渲染方式 
		 * @return 渲染方式
		 * 
		 */		
		protected static function textRendererFactory():TextFieldTextRenderer
		{
			return new TextFieldTextRenderer();
		}
		
		/**
		 * 文本编辑方式 
		 * @return 编辑方式
		 * 
		 */		
		protected static function textEditorFactory():StageTextTextEditor
		{
			return new StageTextTextEditor();
		}
		
		/**
		 * Screen类型通用处理 
		 * @param screen Screen
		 * 
		 */		
		protected function screenInitializer(screen:Screen):void
		{
			screen.originalDPI = this._originalDPI;
		}
		
		/**
		 * 小标题Label样式 
		 * @param label Label
		 * 
		 */		
		protected function titleLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.titleTextFormat;
		}
		
		/**
		 * 居中小标题Label样式 
		 * @param label Label
		 * 
		 */	
		protected function centerTitleLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.centerTitleTextFormat;
		}
		
		/**
		 * 大标题Label样式 
		 * @param label Label
		 * 
		 */	
		protected function largeTitleLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.largeTitleTextFormat;
		}
		
		/**
		 * 居中大标题Label样式 
		 * @param label Label
		 * 
		 */	
		protected function largeCenterTitleLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.largeCenterTitleTextFormat;
		}
		
		/**
		 * 内容Label样式
		 * @param label Label
		 * 
		 */	
		protected function contentLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.contentTextFormat;
		}
		
		/**
		 * 居中内容Label样式
		 * @param label Label
		 * 
		 */	
		protected function centerContentLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.centerContentTextFormat;
		}
		
		/**
		 * 重要内容Label样式
		 * @param label Label
		 * 
		 */	
		protected function importantContentLabelInitializer( label:Label ):void
		{
			label.textRendererProperties.textFormat = this.importantContentTextFormat;
		}
		
		/**
		 * TextArea样式
		 * @param textArea TextArea样式
		 * 
		 */
		protected function textAreaInitializer( textArea:TextArea ):void
		{
			textArea.textEditorProperties.textFormat = this.contentTextFormat;
		}
		
		/**
		 *  ScrollText样式
		 * @param text
		 * 
		 */		
		protected function scrollTextInitializer(text:ScrollText):void
		{
			text.textFormat = this.contentTextFormat;
		}
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理当主场景加入舞台后派发的事件 
		 * @param event 当主场景加入舞台后派发的事件 
		 * 
		 */		
		protected function root_addedToStageHandler(event:Event):void
		{
			this.root.removeEventListener(Event.ADDED_TO_STAGE, root_addedToStageHandler);
			this.initializeRoot();
		}
		
		/**
		 * 处理舞台大小改变后派发的事件 
		 * @param event 舞台大小改变后派发的事件 
		 * 
		 */		
		protected function stage_resizeHandler(event:ResizeEvent):void
		{
			this.primaryBackground.width = event.width;
			this.primaryBackground.height = event.height;
		}
		
		/**
		 * 处理主场景从舞台移除时派发的事件 
		 * @param event 主场景从舞台移除时派发的事件 
		 * 
		 */		
		protected function root_removedFromStageHandler(event:Event):void
		{
			this.root.removeEventListener(Event.REMOVED_FROM_STAGE, root_removedFromStageHandler);
			this.root.stage.removeEventListener(ResizeEvent.RESIZE, stage_resizeHandler);
			this.root.removeChild(this.primaryBackground, true);
			this.primaryBackground = null;
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			if(this.root)
			{
				this.root.removeEventListener(Event.ADDED_TO_STAGE, root_addedToStageHandler);
				if(this.primaryBackground)
				{
					this.root.stage.removeEventListener(ResizeEvent.RESIZE, stage_resizeHandler);
					this.root.removeEventListener(Event.REMOVED_FROM_STAGE, root_removedFromStageHandler);
					this.root.removeChild(this.primaryBackground, true);
					this.primaryBackground = null;
				}
			}
			if(this.atlas)
			{
				this.atlas.dispose();
				this.atlas = null;
			}
			if(this.atlasBitmapData)
			{
				this.atlasBitmapData.dispose();
				this.atlasBitmapData = null;
			}
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function CustomTheme(topLevelContainer:DisplayObjectContainer)
		{
			super(topLevelContainer);
			this.initialize();
		}
	}
}